home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 22 / Cream of the Crop 22.iso / program / cgazv4n2.zip / CPP.ZIP / FILELIST.CXX < prev    next >
C/C++ Source or Header  |  1989-08-26  |  3KB  |  129 lines

  1. // FILELIST.CXX
  2. #include "filelist.hxx"
  3. #include <direct.h>
  4.  
  5. #ifndef __ZTC__
  6. #error Unmodified code may not compile/link on systems other than Zortech
  7. #error because of the function calls to findfirst(), findnext(), getcwd()...
  8. #endif
  9.  
  10. void directory_item::display(display_format format) {
  11.   printf("%-14s", name);
  12.   if (format & sizes) printf("%-8d", size);
  13.   if (format & times) ; // Add code to show time
  14.   if (format & dates) ; // Add code to show date
  15.   if (format & attributes) ; // Add code to show attributes
  16. }
  17.  
  18. subdir::subdir(FIND * p, char * afn) : (p) {
  19.   expanded = 0;  // subdirectory is not expanded
  20.   strcpy(pattern, afn);
  21.   char buf[150];
  22.   getcwd(buf, 149);  // store path information
  23.   strlwr(buf);  // lower case
  24.   path = new char[strlen(buf) + 1];
  25.   strcpy(path, buf);
  26. }
  27.  
  28. subdir::~subdir() {
  29.   delete subdir_list;  // no effect if it's NULL
  30.   delete path;  // ditto
  31. }
  32.  
  33. void subdir::expand() {
  34.   if ( *name != '.') {  // don't create subdirs for . or ..
  35.     chdir(name);
  36.     printf("expanding %s\\%s\n", path, name);
  37.     subdir_list = new file_list(pattern);
  38.     subdir_list->expand_list();  // expand any subdirectories
  39.     chdir("..");
  40.     expanded = 1; // subdirectory is now expanded
  41.   }
  42. }
  43.  
  44. void subdir::display(display_format format) {
  45.   if (format & subdirs) {
  46.     if( *name != '.') {
  47.       printf("\n%s\\%s\n", path, name);
  48.       if(expanded) {
  49.         subdir_list->display_all(format);
  50.         putchar('\n');
  51.       }
  52.     }
  53.   } else {
  54.     printf("\\%-13s", name);
  55.   }
  56. }
  57.  
  58. unsigned long subdir::bytes() {
  59.   if (!expanded) return 0;
  60.   char buf[165];
  61.   sprintf(buf, "%s\\%s", path, name);
  62.   return subdir_list->disk_space(buf);
  63. }
  64.  
  65. void file::display(display_format format) {
  66.   if(format & subdirs) return;  // quit if this is for subdirs
  67.   if (attribute & FA_RDONLY) putchar('(');
  68.   if (attribute & FA_HIDDEN) putchar('[');
  69.   if (attribute & FA_SYSTEM) { putchar('$'); putchar(' '); }
  70.   directory_item::display(format);
  71.   if (attribute & FA_RDONLY) putchar(')');
  72.   if (attribute & FA_HIDDEN) putchar(']');
  73. }
  74.  
  75. file_list::file_list(char * afn) : () {
  76.   // find all different file entries:
  77.   FIND * p = findfirst(afn,
  78.     FA_RDONLY | FA_HIDDEN | FA_SYSTEM | FA_LABEL | FA_DIREC);
  79.   while(p) {
  80.     switch(p->attribute) {
  81.       case FA_DIREC : 
  82.           append(new subdir(p, afn)); // all directories at end
  83.           break;
  84.       default :
  85.           insert(new file(p));  // all files at beginning
  86.     }
  87.     p = findnext();
  88.   }
  89. }
  90.  
  91. void file_list::expand_list() {
  92.   // expand all the subdirectories:
  93.   reset();
  94.   while(!end()) {
  95.     value()->expand();
  96.     next();
  97.   }
  98. }  
  99.  
  100. unsigned long file_list::disk_space(char * msg) {
  101.   reset();
  102.   unsigned long space = 0;
  103.   while(!end()) {
  104.     space += value()->bytes();
  105.     next();
  106.   }
  107.   if(*msg) printf("%s : %lu\n", msg, space);
  108.   return space;
  109. }
  110.  
  111. void file_list::display_all(display_format format) {
  112.   reset();
  113.   int i = 0;
  114.   display_format no_subdirs = format & (~subdirs); // turn off subdirs bit
  115.   while(!end()) {
  116.     value()->display(no_subdirs);
  117.     next();
  118.     if ( (++i % 4) == 0 )
  119.     putchar('\n');
  120.   }
  121.   if(format & subdirs) {
  122.     reset();
  123.     while(!end()) {
  124.       value()->display(format);
  125.       next();
  126.     }
  127.   }
  128. }
  129.